home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / AppInstall / Menu.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  12.9 KB  |  382 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gtk
  5. import gtk.gdk as gtk
  6. import gobject
  7. import xdg.Menu as xdg
  8. import sys
  9. import os
  10. import gettext
  11. import gst
  12. from warnings import warn
  13. from gettext import gettext as _
  14. from Util import *
  15. from CoreMenu import *
  16. (SHOW_ALL, SHOW_ONLY_FREE, UNUSED_1, SHOW_ONLY_SUPPORTED, SHOW_ONLY_THIRD_PARTY, UNUSED_2, SHOW_ONLY_INSTALLED, SHOW_ONLY_MAIN, SHOW_ONLY_PROPRIETARY) = range(9)
  17. PIMP_APPS = [
  18.     'gstreamer0.10-plugins-ugly',
  19.     'gstreamer0.10-ffmpeg',
  20.     'sun-java5-plugin',
  21.     'flashplugin-nonfree',
  22.     'ubuntu-restricted-extras']
  23.  
  24. class NullActivationStyleForMenu:
  25.     
  26.     def __init__(self):
  27.         self.selectFilter = None
  28.         self.menuFilter = None
  29.  
  30.     
  31.     def isApproved(self, component, pkgname):
  32.         return True
  33.  
  34.     
  35.     def menuCacheName(self):
  36.         return 'menu.p'
  37.  
  38.  
  39.  
  40. class ApplicationMenu(CoreApplicationMenu):
  41.     ''' this represents the application menu, the interessting bits are:
  42.         - store that can be attached to a TreeView
  43.         - pkg_to_app a dictionary that maps the apt pkgname to the application
  44.                      items
  45.     '''
  46.     debug = 0
  47.     
  48.     def __init__(self, datadir, cachedir, cache, treeview_packages, progress, filter = SHOW_ONLY_SUPPORTED, dontPopulate = False, activation_style = NullActivationStyleForMenu()):
  49.         CoreApplicationMenu.__init__(self, datadir)
  50.         self.cache = cache
  51.         self.treeview_packages = treeview_packages
  52.         self.activationStyle = activation_style
  53.         self.icons = gtk.icon_theme_get_default()
  54.         self.icons.append_search_path(os.path.join(datadir, 'icons'))
  55.         self.searchTerms = []
  56.         if self.activationStyle.menuFilter is not None:
  57.             self.filter = self.activationStyle.menuFilter
  58.         else:
  59.             self.filter = filter
  60.         self.real_categories_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
  61.         if dontPopulate:
  62.             return None
  63.         cacheLoaded = False
  64.         cname = activation_style.menuCacheName()
  65.         if not cacheLoaded:
  66.             progress.label_action.set_label(_('Collecting application data...'))
  67.             self.desktopEntriesSeen.clear()
  68.             menu = xdg.Menu.parse(os.path.join(self.menudir, 'applications.menu'))
  69.             self._populateFromEntry(menu)
  70.         
  71.         self.refresh(progress)
  72.         self.store = self.real_categories_store
  73.  
  74.     
  75.     def get_categories_store(self):
  76.         return self.real_categories_store
  77.  
  78.     
  79.     def _refilter(self, model = None):
  80.         if not model:
  81.             model = self.treeview_packages.get_model()
  82.         
  83.         name = None
  84.         (path, colum) = self.treeview_packages.get_cursor()
  85.         if path:
  86.             
  87.             try:
  88.                 name = model.get_value(model.get_iter(path), COL_NAME)
  89.             except ValueError:
  90.                 e = None
  91.             except:
  92.                 None<EXCEPTION MATCH>ValueError
  93.             
  94.  
  95.         None<EXCEPTION MATCH>ValueError
  96.         self.treeview_packages.set_model(None)
  97.         if model != None:
  98.             model.get_model().refilter()
  99.         
  100.         self.treeview_packages.set_model(model)
  101.         if name != None:
  102.             for it in iterate_list_store(model, model.get_iter_first()):
  103.                 aname = model.get_value(it, COL_NAME)
  104.                 if name == aname:
  105.                     self.treeview_packages.set_cursor(model.get_path(it))
  106.                     return None
  107.             
  108.         elif len(model) > 0:
  109.             self.treeview_packages.set_cursor(0)
  110.         
  111.  
  112.     
  113.     def _name_sort_func(self, model, iter1, iter2):
  114.         '''
  115.         Sort by name, honor special craziness
  116.         '''
  117.         item1 = model.get_value(iter1, COL_ITEM)
  118.         item2 = model.get_value(iter2, COL_ITEM)
  119.         if item1 == None or item2 == None:
  120.             return 0
  121.         cat = model.get_data('category')
  122.         if cat and cat.name != self.all_category_name and hasattr(item1, 'onTop') and hasattr(item2, 'onTop'):
  123.             if not item1.onTop and item2.onTop:
  124.                 if item1.onTop:
  125.                     return -1
  126.                 if item2.onTop:
  127.                     return 1
  128.             
  129.         
  130.         name1 = model.get_value(iter1, COL_NAME)
  131.         name2 = model.get_value(iter2, COL_NAME)
  132.         if name1 < name2:
  133.             return -1
  134.         if name1 > name2:
  135.             return 1
  136.         return 0
  137.  
  138.     
  139.     def _ranking_sort_func(self, model, iter1, iter2):
  140.         '''
  141.         Sort by the search result rank
  142.         '''
  143.         item1 = model.get_value(iter1, COL_ITEM)
  144.         item2 = model.get_value(iter2, COL_ITEM)
  145.         if item1 == None or item2 == None:
  146.             return 0
  147.         if item1.rank < item2.rank:
  148.             return 1
  149.         if item1.rank > item2.rank:
  150.             return -1
  151.         return 0
  152.  
  153.     
  154.     def _visible_filter(self, model, iter):
  155.         item = model.get_value(iter, COL_ITEM)
  156.         return True
  157.  
  158.     
  159.     def _filterAndRank(self, item):
  160.         '''
  161.         Watch out, Google!
  162.         '''
  163.         trigger = ''
  164.         rank = 100 * item.popcon / self.popcon_max
  165.         for term in self.searchTerms:
  166.             hit = False
  167.             if term == item.name.lower() or term == item.pkgname.lower():
  168.                 rank += 100
  169.                 hit = True
  170.             
  171.             if term in item.name.lower():
  172.                 rank += 30
  173.                 trigger += ' name'
  174.                 hit = True
  175.             
  176.             if term in item.pkgname.lower():
  177.                 rank += 30
  178.                 trigger += ' pkg_name'
  179.                 hit = True
  180.             
  181.             if self._mimeMatch(item, term, fuzzy = True):
  182.                 rank += 25
  183.                 trigger += ' mime'
  184.                 hit = True
  185.             
  186.             if self._codecMatch(item, term, fuzzy = True):
  187.                 rank += 15
  188.                 trigger += ' codec'
  189.                 hit = True
  190.             
  191.             if self.cache.has_key(item.pkgname) and term in self.cache[item.pkgname].description.lower():
  192.                 rank += 10
  193.                 trigger += ' pkg_desc'
  194.                 hit = True
  195.             
  196.             if hit == False:
  197.                 return None
  198.             if item.pkgname.lower() in PIMP_APPS:
  199.                 rank += 75
  200.                 continue
  201.             hit == False
  202.         
  203.         return rank
  204.  
  205.     
  206.     def _mimeMatch(self, item, term, fuzzy = False):
  207.         for pattern in item.mime:
  208.             if fuzzy and term in pattern:
  209.                 return True
  210.             if not fuzzy and pattern == term:
  211.                 return True
  212.         
  213.         return False
  214.  
  215.     
  216.     def _codecMatch(self, item, term, fuzzy = False):
  217.         if ':' in term:
  218.             term = term.split(':')[1]
  219.         
  220.         search_cap = gst.caps_from_string(term)
  221.         for codec in item.codecs:
  222.             if ':' in codec:
  223.                 codec = codec.split(':')[1]
  224.             
  225.             if fuzzy and term in codec:
  226.                 return True
  227.             cap = gst.caps_from_string(codec)
  228.             if cap and search_cap and search_cap & cap:
  229.                 return True
  230.         
  231.         return False
  232.  
  233.     
  234.     def _activationStyleFilter(self, item):
  235.         filter = self.activationStyle.selectFilter(self)
  236.         if self.activationStyle.isInstallerOnly and self.itemIsInstalled(item):
  237.             return False
  238.         for term in self.activationStyle.searchTerms():
  239.             if filter(item, term):
  240.                 return True
  241.         
  242.         return False
  243.  
  244.     
  245.     def doMimeSearch(self, mime_type, fuzzy = False):
  246.         res = set()
  247.         model = self.real_categories_store.get_value(self.all_category_iter, COL_CAT_ITEM).all_applications
  248.         for it in iterate_list_store(model, model.get_iter_first()):
  249.             item = model.get_value(it, COL_ITEM)
  250.             for re_pattern in item.mime:
  251.                 pattern = re_pattern.pattern
  252.                 if fuzzy and mime_type in pattern:
  253.                     res.add(item)
  254.                     continue
  255.                 if not fuzzy and re_pattern.match(mime_type):
  256.                     res.add(item)
  257.                     continue
  258.             
  259.         
  260.         return res
  261.  
  262.     
  263.     def refreshAfterCacheChange(self, progress):
  264.         for cat in self.pickle:
  265.             for item in self.pickle[cat]:
  266.                 if self.cache.has_key(item.pkgname):
  267.                     pass
  268.                 item.toInstall = self.cache[item.pkgname].isInstalled
  269.             
  270.         
  271.  
  272.     
  273.     def refresh(self, progress):
  274.         self.real_categories_store.clear()
  275.         progress.subOp = _('Loading applications...')
  276.         progress.update(0)
  277.         self.all_category_iter = self.real_categories_store.append()
  278.         self.all_category_name = '<b>%s</b>' % _('All')
  279.         item = Category(self, self.all_category_name, 'distributor-logo')
  280.         self.initListStores(item, self)
  281.         self.real_categories_store.set(self.all_category_iter, COL_CAT_NAME, '<b>%s</b>' % _('All'), COL_CAT_ITEM, item)
  282.         i = 0
  283.         lenx = len(self.pickle.keys())
  284.         keys = self.pickle.keys()
  285.         keys.sort(cmp = (lambda x, y: cmp(x.name.lower(), y.name.lower())))
  286.         for category in keys:
  287.             progress.subOp = _('Loading %s...') % category.name
  288.             self.initListStores(category, self)
  289.             self.real_categories_store.set(self.real_categories_store.append(), COL_CAT_NAME, category.name, COL_CAT_ITEM, category)
  290.             i += 1
  291.             progress.update((i / float(lenx)) * 100)
  292.             for item in self.pickle[category]:
  293.                 category.all_applications.append([
  294.                     item.name,
  295.                     item,
  296.                     item.popcon])
  297.                 store = self.real_categories_store.get_value(self.all_category_iter, COL_ITEM).all_applications
  298.                 store.append([
  299.                     item.name,
  300.                     item,
  301.                     item.popcon])
  302.                 if item.popcon > self.popcon_max:
  303.                     self.popcon_max = item.popcon
  304.                 
  305.                 pkgname = item.pkgname
  306.                 if not self.pkg_to_app.has_key(pkgname):
  307.                     self.pkg_to_app[pkgname] = set()
  308.                 
  309.                 self.pkg_to_app[pkgname].add(item)
  310.             
  311.         
  312.         self.refreshAfterCacheChange(progress)
  313.  
  314.     
  315.     def itemAvailable(self, item):
  316.         ''' returns True if the item is available in the current
  317.             apt cache '''
  318.         return self.cache.has_key(item.pkgname)
  319.  
  320.     
  321.     def itemIsInstalled(self, item):
  322.         ''' returns True if the item is currently installed '''
  323.         if self.cache.has_key(item.pkgname):
  324.             pass
  325.         return self.cache[item.pkgname].isInstalled
  326.  
  327.     
  328.     def initListStores(self, category, parent):
  329.         category.all_applications = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT, gobject.TYPE_INT)
  330.         category.filtered_applications = category.all_applications.filter_new()
  331.         category.filtered_applications.set_visible_func(parent._visible_filter)
  332.         category.filtered_applications.set_data('category', category)
  333.         category.applications = gtk.TreeModelSort(category.filtered_applications)
  334.         category.applications.set_data('category', category)
  335.  
  336.     
  337.     def getChanges(self, get_paths = False):
  338.         ''' return the selected changes in the tree
  339.             TODO: what is get_paths?
  340.         '''
  341.         to_inst = set()
  342.         to_rm = set()
  343.         for name, item in self.store:
  344.             for name, item, popcon in item.all_applications:
  345.                 if self.itemIsInstalled(item) and not (item.toInstall):
  346.                     to_rm.add(item)
  347.                 
  348.                 if not self.itemIsInstalled(item) and item.toInstall:
  349.                     to_inst.add(item)
  350.                     continue
  351.             
  352.         
  353.         return (to_inst, to_rm)
  354.  
  355.     
  356.     def isChanged(self):
  357.         ''' check if there are changes at all '''
  358.         for cat_name, cat in self.store:
  359.             for name, item, popcon in cat.all_applications:
  360.                 if item.toInstall != self.itemIsInstalled(item):
  361.                     return True
  362.             
  363.         
  364.         return False
  365.  
  366.     
  367.     def _dbg(self, level, msg):
  368.         '''Write debugging output to sys.stderr.'''
  369.         if level <= self.debug:
  370.             print >>sys.stderr, msg
  371.         
  372.  
  373.  
  374. if __name__ == '__main__':
  375.     print 'testing the menu'
  376.     desktopdir = '/usr/share/app-install'
  377.     from Util import MyCache
  378.     cache = MyCache()
  379.     treeview = gtk.TreeView()
  380.     menu = ApplicationMenu(desktopdir, cache, treeview, treeview, apt.progress.OpProgress())
  381.  
  382.